package com.ubtechinc.cruzr.launcher2.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ubtechinc.cruzr.launcher2.R;
import com.ubtechinc.cruzr.launcher2.callback.RxTimerListener;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.launcher2.utlis.RxTimer;

public class ThirdAppBar {
    private WindowManager mWindowManager;
    private Context mContext;
    private WindowManager.LayoutParams mParams;
    private RelativeLayout mShowLayout;
    private ImageView mHide;
    private boolean isTouching = false;
    private InputMethodManager mImm;
    /**
     * 屏幕高度
     */
    private int mScreenHeight = 1080;
    /**
     * Bar的高度
     */
    private int mBarHeight = 96;
    private int mLastDy = 0;

    public void initShowBar(Context context, int id) {
        mContext = context;
        initParams();
        mShowLayout = (RelativeLayout) LayoutInflater.from(context).inflate(id, null);
        createShowManager(mShowLayout);
        moveIcon();
        mImm = InputMethodManager.getInstance();
    }

    @SuppressLint("RtlHardcoded")
    private void initParams() {
        DisplayMetrics dm2 = mContext.getResources().getDisplayMetrics();
        mScreenHeight = dm2.heightPixels;
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mParams = new WindowManager.LayoutParams();
        mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mParams.y = 917;
        mParams.x = 1744;
        mLastDy = mParams.y;
        mParams.format = PixelFormat.RGBA_8888;
        mParams.gravity = Gravity.TOP | Gravity.LEFT;
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    }

    private void createShowManager(View v) {
        mWindowManager.addView(v, mParams);
        mHide = (ImageView) v.findViewById(R.id.hide_image);
        mHide.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                if (mListener != null) {
                    mListener.onClose();
                }
            }
        });
    }

    private void changeViewPosition() {
        final RxTimer timer = new RxTimer();
        LogUtils.e("rqh the mParams dsfdsfd");
        timer.startTime(new RxTimerListener() {
            @Override
            public void onTimeNext() {
                timer.closeTimer();
                if (isTouching) {
                    return;
                }
                int dy = getKeyBroadHeight();
                int py = mScreenHeight - dy - mBarHeight;
                LogUtils.e("rqh the mParams = " + py + " " + dy + " " + mLastDy);
                if (py >= mLastDy && (dy != 0)) {
                    return;
                }
                if (dy > 0) {
                    mParams.y = py;
                    mWindowManager.updateViewLayout(mShowLayout, mParams);
                } else {
                    mParams.y = mLastDy;
                    mWindowManager.updateViewLayout(mShowLayout, mParams);
                }
            }
        }, 500, 500);
    }

    @SuppressLint("ClickableViewAccessibility")
    private void moveIcon() {
        mHide.setOnTouchListener(new View.OnTouchListener() {
            int lastY, lastX;
            int paramY, paramX;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        isTouching = true;
                        lastY = (int) event.getRawY();
                        lastX = (int) event.getRawX();
                        paramY = mParams.y;
                        paramX = mParams.x;
                        mLastDy = mParams.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dy = (int) event.getRawY() - lastY;
                        mParams.y = paramY + dy;
                        mLastDy = mParams.y;
                        mWindowManager.updateViewLayout(mShowLayout, mParams);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        isTouching = false;
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
    }

    public void recoverView() {
        mParams.y = mScreenHeight - mBarHeight;
        mWindowManager.updateViewLayout(mShowLayout, mParams);
    }

    public void refreshView() {
        changeViewPosition();
    }

    private int getKeyBroadHeight() {
        return mImm.getInputMethodWindowVisibleHeight() + 60;
    }


    private onWindowClick mListener;

    public void setOnWindowClick(onWindowClick l) {
        mListener = l;
    }

    public interface onWindowClick {
        void onClose();
    }

    public void hide() {
        mShowLayout.setVisibility(View.GONE);
    }

    public void show() {
        mShowLayout.setVisibility(View.VISIBLE);
        mParams.y = 917;
        mParams.x = 1744;
        mWindowManager.updateViewLayout(mShowLayout, mParams);
    }
}
